home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1987 / 11 / holublst.nov < prev    next >
Text File  |  1987-10-08  |  15KB  |  400 lines

  1.                   Listing 1 -- calendar.c, Printed 12/31/1987
  2.    ____________________________________________________________________________
  3.   1| #include <stdio.h>
  4.   2| #include <time.h>
  5.   3| #include <ctype.h>
  6.   4| /*------------------------------------------------------------
  7.   5|  * CALENDAR.C   This program searches a file called "calendar"
  8.   6|  *              in the current directory and prints all lines
  9.   7|  * that start with today's or tomorrow's date. Note that this
  10.   8|  * behaviour differs from Unix, which allows the date to be
  11.   9|  * anywhere on the line. Leading whitespace on the line is
  12.  10|  * ignored, however. Like Unix, "tomorrow" is extended to
  13.  11|  * Monday if calandar is run on a weekend.
  14.  12|  *
  15.  13|  * If the date is a weekday (monday, tuesday, wednesday),
  16.  14|  * which can be abbreviated by the first few character of the
  17.  15|  * name (mon, tu, wed) then the line is printed on that day,
  18.  16|  * reguardless of the actual date. For example, lines starting
  19.  17|  * with "tue" will be printed every tuesday. Explicit dates
  20.  18|  * can be listed too and most common abreviations work:
  21.  19|  *
  22.  20|  *              Sept. 7, 1987
  23.  21|  *              September 7 '87
  24.  22|  *              sept. 7
  25.  23|  *              Sep 7 87
  26.  24|  *              9-7
  27.  25|  *              9/7
  28.  26|  *              9/7/87
  29.  27|  *              9 7 1987
  30.  28|  *              9-7-1987
  31.  29|  *
  32.  30|  * and so forth. Like unix, "7 September" doesn't work, however.
  33.  31|  * The day must follow the month.
  34.  32|  *
  35.  33|  * If a * is found in the month's section of any of the above
  36.  34|  * dates (as in "*-7-87" or "* 7)" then that day in any month
  37.  35|  * will match. If no day is specified, then all days in the
  38.  36|  * indicated month will match.
  39.  37|  *
  40.  38|  * Abbreviations are formed by looking at the first few
  41.  39|  * characters of the word. The shortest possible abbreviations
  42.  40|  * are:
  43.  41|  *
  44.  42|  *              su      sunday          mo      monday
  45.  43|  *              tu      tuesday         w       wednesday
  46.  44|  *              th      thursday        fr      friday
  47.  45|  *              sa      saturday        ja      january
  48.  46|  *              fe      february        mar     march
  49.  47|  *              ap      april           may     may
  50.  48|  *              jun     june            jul     july
  51.  49|  *              au      august          se      september
  52.  50|  *              o       october         n       novemeber
  53.  51|  *              d       december
  54.  52|  *
  55.  53|  * BUGS: * If you specify both the day of the week and the date,
  56.  54|  *         as in "Tuesday, July 7, 1987", the "Tuesday" is 
  57.  55|  *         processed and the date is ignored.
  58.  56|  *
  59.  57|  *       * The following won't work:
  60.  58|  *                      Jun 1   49er's today
  61.  59|  *        because calendar will pick off 49 as the current year.
  62.  60|  *        Put some nonnumeric character that can't be part of a
  63.  61|  *        date string in front of the "49." You can use any
  64.  62|  *        character but a number or one of: { - . / ' }.
  65.  63|  */
  66.  64| 
  67.  65| #define JAN     31      /* Days in the month    */
  68.  66| #define FEB     28
  69.  67| #define MAR     31
  70.  68| #define APR     30
  71.  69| #define MAY     31
  72.  70| #define JUN     30
  73.  71| #define JUL     31
  74.  72| #define AUG     31
  75.  73| #define SEP     30
  76.  74| #define OCT     31
  77.  75| #define NOV     30
  78.  76| #define DEC     31
  79.  77| 
  80.  78| int offset_to_first_of_month[] =
  81.  79| {
  82.  80|         0,                      /* Offset to the first of */
  83.  81|         JAN,                        /* of the month from  */
  84.  82|         JAN +FEB,                       /* January, 1st.  */
  85.  83|         JAN +FEB +MAR,
  86.  84|         JAN +FEB +MAR +APR,
  87.  85|         JAN +FEB +MAR +APR +MAY,
  88.  86|         JAN +FEB +MAR +APR +MAY +JUN,
  89.  87|         JAN +FEB +MAR +APR +MAY +JUN +JUL,
  90.  88|         JAN +FEB +MAR +APR +MAY +JUN +JUL +AUG,
  91.  89|         JAN +FEB +MAR +APR +MAY +JUN +JUL +AUG +SEP,
  92.  90|         JAN +FEB +MAR +APR +MAY +JUN +JUL +AUG +SEP +OCT,
  93.  91|         JAN +FEB +MAR +APR +MAY +JUN +JUL +AUG +SEP +OCT +NOV
  94.  92| };
  95.  93| 
  96.  94| /*------------------------------------------------------------*/
  97.  95| 
  98.  96| char    *skipstuff(p)
  99.  97| char    *p;
  100.  98| {
  101.  99|     /* All of the following can be used in a date string */
  102. 100| 
  103. 101|     while(isspace(*p)||*p=='-'||*p=='/'||*p=='.'||*p==','
  104. 102|                                                 ||*p=='\'')
  105. 103|         ++p;
  106. 104| 
  107. 105|     return p;
  108. 106| }
  109. 107| 
  110. 108| /*------------------------------------------------------------*/
  111. 109| 
  112. 110| main()
  113. 111| {
  114. 112|     char        buf[132], *p;
  115. 113|     FILE        *fd;
  116. 114|     struct tm   *t;
  117. 115|     long        thetime;
  118. 116|     int         month, day, year, wday;
  119. 117|     int         c1, c2, c3;
  120. 118| 
  121. 119|     if( !(fd = fopen("calendar", "r") ) )
  122. 120|     {
  123. 121|         perror( "calandar" );
  124. 122|         exit(1);
  125. 123|     }
  126. 124| 
  127. 125|     thetime = time(NULL) ;
  128. 126|     t = localtime( &thetime );
  129. 127|     printf("Today is %s\n", asctime( t ) );
  130. 128| 
  131. 129|     while( fgets( buf, 132, fd ) )
  132. 130|     {
  133. 131|         wday  = -1;
  134. 132|         month = -1;
  135. 133|         day   =  0;
  136. 134|         year  =  0;
  137. 135|         p     = buf;
  138. 136| 
  139. 137|         while( isspace(*p) )
  140. 138|                 ++p;
  141. 139| 
  142. 140|         if( isalpha(*p) )
  143. 141|         {
  144. 142|             c1 = tolower( p[0] );
  145. 143|             c2 = tolower( p[1] );
  146. 144|             c3 = tolower( p[2] );
  147. 145| 
  148. 146|             if     ( c1=='a' && c2=='p'            ) month = 3 ;
  149. 147|             else if( c1=='a' && c2=='u'            ) month = 7 ;
  150. 148|             else if( c1=='d'                       ) month = 11;
  151. 149|             else if( c1=='f' && c2=='e'            ) month = 1 ;
  152. 150|             else if( c1=='f' && c2=='r'            ) wday  = 5 ;
  153. 151|             else if( c1=='j' && c2=='a'            ) month = 0 ;
  154. 152|             else if( c1=='j' && c2=='u' && c3=='l' ) month = 6 ;
  155. 153|             else if( c1=='j' && c2=='u' && c3=='n' ) month = 5 ;
  156. 154|             else if( c1=='m' && c2=='a' && c3=='r' ) month = 2 ;
  157. 155|             else if( c1=='m' && c2=='a' && c3=='y' ) month = 4 ;
  158. 156|             else if( c1=='m' && c2=='o'            ) wday  = 1 ;
  159. 157|             else if( c1=='n'                       ) month = 10;
  160. 158|             else if( c1=='o'                       ) month = 9 ;
  161. 159|             else if( c1=='s' && c2=='a'            ) wday  = 6 ;
  162. 160|             else if( c1=='s' && c2=='e'            ) month = 8 ;
  163. 161|             else if( c1=='s' && c2=='u'            ) wday  = 0 ;
  164. 162|             else if( c1=='t' && c2=='h'            ) wday  = 4 ;
  165. 163|             else if( c1=='t' && c2=='u'            ) wday  = 2 ;
  166. 164|             else if( c1=='w'                       ) wday  = 3 ;
  167. 165| 
  168. 166|             if( wday >0 )
  169. 167|             {
  170. 168|                 if( t->tm_wday == wday || (t->tm_wday +1) == wday)
  171. 169|                     fputs( buf, stdout );
  172. 170| 
  173. 171|                 continue;
  174. 172|             }
  175. 173| 
  176. 174|             while( isalpha( *p ) || *p == '.' )
  177. 175|                     p++;
  178. 176| 
  179. 177|             p = skipstuff(p);
  180. 178|         }
  181. 179| 
  182. 180|         if( isdigit( *p ) || *p == '*' )
  183. 181|         {
  184. 182|             if( *p == '*' )
  185. 183|             {
  186. 184|                 month = t->tm_mon;
  187. 185|                 ++p;
  188. 186|             }
  189. 187|             else if( month < 0 )
  190. 188|                 month = stoi( &p ) - 1;
  191. 189| 
  192. 190|             p   = skipstuff(  p );
  193. 191|             day = stoi     ( &p );
  194. 192|             p   = skipstuff(  p );
  195. 193| 
  196. 194|             if( (year = stoi(&p)) > 99 )
  197. 195|                     year -= 1900;
  198. 196|         }
  199. 197| 
  200. 198|         if( !year     )   year  = t->tm_year ;
  201. 199|         if( month < 0 )   month = t->tm_mon  ;
  202. 200|         if( !day      )   day   = t->tm_mday ;
  203. 201| 
  204. 202|         /* Convert the specified date to an offset, in days,
  205. 203|          * from Jan 1. Unix provides the offset for today in the
  206. 204|          * tm_yday field. It makes life difficult by making 1 Jan.
  207. 205|          * be 0, however. We also have to compensate for leap
  208. 206|          * year. If the year is a multiple of 4, it's a leap year
  209. 207|          * unless it's also the first year of the century (1900
  210. 208|          * was not a leap year, even though it's a multiple of 4).
  211. 209|          */
  212. 210| 
  213. 211|         day += offset_to_first_of_month[ month ] - 1;
  214. 212|         if( year % 4 == 0  &&  year % 100 != 0  && month >= 2 )
  215. 213|                 day++;
  216. 214| 
  217. 215|         /* Print the line if required. The first test is the
  218. 216|          * normal situation (today or tomorrow). The second
  219. 217|          * test takes care of Saturday, which must include
  220. 218|          * the following Monday in its definition of "tomorrow."
  221. 219|          * The third test takes care of December 31, which must
  222. 220|          * recognize January 1 as "tomorrow." Note that I'm
  223. 221|          * intentionally not testing for the years being different
  224. 222|          * because it seems reasonable that, if no year is
  225. 223|          * specified and today is December 31, that the next year
  226. 224|          * is implied by January 1, with no date.
  227. 225|          */
  228. 226| 
  229. 227|         if( t->tm_year == year &&
  230. 228|                     (day == t->tm_yday || day == t->tm_yday +1) )
  231. 229|             fputs( buf, stdout );
  232. 230| 
  233. 231|         if( t->tm_wday == 6  &&  day == t->tm_yday + 2 )
  234. 232|             fputs( buf, stdout );
  235. 233| 
  236. 234|         if( t->tm_mday == 31 && t->tm_mon == 11 && day == 0 )
  237. 235|             fputs( buf, stdout );
  238. 236|     }
  239. 237| }
  240.                     
  241.  
  242.  
  243.  
  244. Listing 2 -- dateh.c, Printed 7/26/1987
  245.    ____________________________________________________________________________
  246.   1| #include <stdio.h>
  247.   2| #include <dos.h>
  248.   3| #include <time.h>
  249.   4| #include <sys/types.h>
  250.   5| #include <sys/stat.h>
  251.   6| 
  252.   7| /*------------------------------------------------------------
  253.   8|  *      Creates or updates file called date.h that looks like:
  254.   9|  *
  255.  10|  *              #define _DAY_     "6-17-87"
  256.  11|  *              #define _TIME_24_ "12:5:23"
  257.  12|  *              #define _TIME_    "12:5 PM"
  258.  13|  *              #define _DATE_    "6-17-87 12:5 PM"
  259.  14|  *
  260.  15|  * and holds the current time and date. The file is  created
  261.  16|  * if it doesn't exist. If it does exist, it is modified, but
  262.  17|  * only if it was not modified or created earlier on the same
  263.  18|  * day. All of this behaviour can be modified by command-line
  264.  19|  * switches [see usage(), below].
  265.  20|  *
  266.  21|  * This program uses the Unix time functions. For them to work,
  267.  22|  * you need to set the various codes in the TZ environment
  268.  23|  * variable. For example:
  269.  24|  *              setenv TZ=PST8PDT               (Unix or Sh)
  270.  25|  *              set    TZ=PST8PDT               (COMMAND.COM)
  271.  26|  *
  272.  27|  * Says the Pacific Standard Time is 8 hours off of Greenwich
  273.  28|  * and the PST is used as the abbreviation for it. PDT is used
  274.  29|  * as an abbreviation for daylight savings time.
  275.  30|  *
  276.  31|  * Exit status is 0 if the file is untouched, 1 if it's created
  277.  32|  * or modified, 2 on an error of some sort.
  278.  33|  */
  279.  34| 
  280.  35| main( argc, argv )
  281.  36| char    **argv;
  282.  37| {
  283.  38|     FILE        *fd;
  284.  39|     struct stat stats;
  285.  40|     struct tm   *t;
  286.  41|     int         day ;
  287.  42|     long        thetime;
  288.  43|     int         pm;
  289.  44|     char        *p;
  290.  45| 
  291.  46|     int         verbose  = 0;
  292.  47|     int         force    = 0;
  293.  48|     int         create   = 0;
  294.  49| 
  295.  50|     if( argc != 1 )
  296.  51|     {
  297.  52|             p = argv[1];
  298.  53| 
  299.  54|             if( *p != '-' )
  300.  55|                 usage();
  301.  56|             else
  302.  57|                 for( ++p ; *p ; p++ )
  303.  58|                     switch( *p )
  304.  59|                     {
  305.  60|                     case 'v': verbose = 1;      break;
  306.  61|                     case 'f': force   = 1;      break;
  307.  62|                     case 'c': create  = 1;      break;
  308.  63|                     default : usage();
  309.  64|                     }
  310.  65|     }
  311.  66| 
  312.  67|     thetime = time(NULL) ;
  313.  68| 
  314.  69|     if( stat("date.h", &stats) != 0 )
  315.  70|     {
  316.  71|         if( !create )           /* File doesn't exist */
  317.  72|             exit( 0 );
  318.  73| 
  319.  74|         t = localtime( &thetime );
  320.  75|         printf("Creating DATE.H: %s\n", asctime(t) );
  321.  76|     }
  322.  77|     else
  323.  78|     {
  324.  79|         t = localtime( &(stats.st_mtime) );
  325.  80| 
  326.  81|         if( verbose )
  327.  82|             printf( "DATE.H last modified: %s", asctime(t) );
  328.  83| 
  329.  84|         day = t->tm_yday;
  330.  85| 
  331.  86|         t = localtime( &thetime );
  332.  87| 
  333.  88|         if( verbose )
  334.  89|             printf( "Today is: %s", asctime(t) );
  335.  90| 
  336.  91|         if( t->tm_yday == day && !force )
  337.  92|             exit( 0 );
  338.  93|         else
  339.  94|             printf("Modifying DATE.H\n");
  340.  95|     }
  341.  96| 
  342.  97|     if( !(fd = fopen( "date.h", "w" )) )
  343.  98|     {
  344.  99|             perror( "date.h" );
  345. 100|             exit( 2 );
  346. 101|     }
  347. 102|     else
  348. 103|     {
  349. 104|         fprintf(fd, "#define _DAY_     \"%d-%d-%d\"\n", 
  350. 105|                                                t->tm_mon,
  351. 106|                                                t->tm_mday,
  352. 107|                                                t->tm_year );
  353. 108| 
  354. 109|         fprintf(fd, "#define _TIME_24_ \"%d:%d:%d\"\n",    
  355. 110|                                                t->tm_hour,
  356. 111|                                                t->tm_min,
  357. 112|                                                t->tm_sec );
  358. 113|         pm = t->tm_hour >= 12;
  359. 114| 
  360. 115|         if( t->tm_hour > 12 )
  361. 116|                 t->tm_hour -= 12;
  362. 117| 
  363. 118|         else if( t->tm_hour == 0 )
  364. 119|                 t->tm_hour  = 12;
  365. 120| 
  366. 121|         fprintf(fd, "#define _TIME_    \"%d:%d %s\"\n",    
  367. 122|                                        t->tm_hour,
  368. 123|                                        t->tm_min,
  369. 124|                                        pm ? "PM" : "AM" );
  370. 125| 
  371. 126|         fprintf(fd, "#define _DATE_    \"%d-%d-%d %d:%d %s\"\n",
  372. 127|                                        t->tm_mon,
  373. 128|                                        t->tm_mday,
  374. 129|                                        t->tm_year,
  375. 130|                                        t->tm_hour,
  376. 131|                                        t->tm_min,
  377. 132|                                        pm ? "PM" : "AM" );
  378. 133|         fclose(fd);
  379. 134|     }
  380. 135| 
  381. 136|     exit( 1 );
  382. 137| }
  383. 138| 
  384. 139| /*------------------------------------------------------------*/
  385. 140| 
  386. 141| #define E(x) fprintf(stderr,x)
  387. 142| 
  388. 143| usage()
  389. 144| {
  390. 145|     E("Usage dateh [-fvc]\n");
  391. 146|     E("If date.h doesn't exist, create it, otherwise if\n");
  392. 147|     E("the date stamp isn't today, update it\n");
  393. 148|     E("\n");
  394. 149|     E("-f  forces an update, even if the date stamp is ok\n");
  395. 150|     E("-v  verbose operation\n");
  396. 151|     E("-c  create file if it doesn't exist\n");
  397. 152|     E("\n");
  398. 153|     exit( 2 );
  399. 154| }
  400.